package com.zz.ecommerce.baseManage.baseDict.service;

import com.zz.ecommerce.baseManage.baseDict.bean.BaseDictBean;
import com.zz.ecommerce.baseManage.baseDict.bean.TDictManager;
import com.zz.ecommerce.baseManage.baseDict.dao.BaseDictDao;
import com.zz.ecommerce.common.CommonDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhangmeng on 2016/3/31.
 */
@Service
public class BaseDictService {
    @Autowired
    CommonDao commonDao;
    @Autowired
    BaseDictDao baseDictDao;

    /**
     * 查询字典列表
     *
     * @param dictManager
     * @param dictBean
     * @param start
     * @param end
     * @param sort
     * @return
     */
    public List<BaseDictBean> pageWhere(TDictManager dictManager, BaseDictBean dictBean, int start, int end, String sort) {
        StringBuffer sqlBuffer = new StringBuffer("SELECT ");
        sqlBuffer.append(generateSelect(dictManager));
        sqlBuffer.append("  FROM (SELECT A.*, ROWNUM RN " +
                "          FROM (SELECT * FROM " + dictManager.getRealTableName() + " where 1 = 1 " + generateLikeSql(dictManager, dictBean) + " " + sort + ") A\n" +
                "         WHERE ROWNUM <= " + end + ") " +
                " WHERE RN >=" + start);
        return baseDictDao.qrySql(sqlBuffer.toString());
    }

    /**
     * 生成查询字段
     *
     * @param dictManager
     * @return
     */
    private String generateSelect(TDictManager dictManager) {
        String sql = "";
        if (StringUtils.isNotEmpty(dictManager.getCodeKey())) {
            sql += dictManager.getCodeKey() + " code,";
        }
        if (StringUtils.isNotEmpty(dictManager.getCnNameKey())) {
            sql += dictManager.getCnNameKey() + " cnName,";
        }
        if (StringUtils.isNotEmpty(dictManager.getEnNameKey())) {
            sql += dictManager.getEnNameKey() + " enName,";
        }
        if (StringUtils.isNotEmpty(dictManager.getRemarkKey())) {
            sql += dictManager.getRemarkKey() + " remark,";
        }
        if (StringUtils.isNotEmpty(dictManager.getEnableKey())) {
            sql += dictManager.getEnableKey() + " enable,";
        }
        if (StringUtils.isNotEmpty(dictManager.getIndexKey())) {
            sql += dictManager.getIndexKey() + " indx,";
        }
        if (sql.endsWith(",")) {
            sql = sql.substring(0, sql.length() - 1);
        }
        return sql;
    }

    /**
     * 查询字典数量
     *
     * @param dictManager
     * @param dictBean
     * @return
     */
    public long countWhere(TDictManager dictManager, BaseDictBean dictBean) {
        StringBuffer sqlBuffer = new StringBuffer("SELECT count(1) from " + dictManager.getRealTableName() + " where 1=1 ");
        sqlBuffer.append(generateLikeSql(dictManager, dictBean));
        return baseDictDao.countSql(sqlBuffer.toString());
    }

    /**
     * 生成查询sql
     *
     * @param dictManager
     * @param dictBean
     * @return
     */
    private String generateLikeSql(TDictManager dictManager, BaseDictBean dictBean) {
        String sql = "";
        if (StringUtils.isNotEmpty(dictManager.getCodeKey()) && StringUtils.isNotEmpty(dictBean.getCode())) {
            sql += " and " + dictManager.getCodeKey() + " like '%" + dictBean.getCode() + "%'";
        }
        if (StringUtils.isNotEmpty(dictManager.getCnNameKey()) && StringUtils.isNotEmpty(dictBean.getCnName())) {
            sql += " and " + dictManager.getCnNameKey() + " like '%" + dictBean.getCnName() + "%'";
        }
        if (StringUtils.isNotEmpty(dictManager.getEnNameKey()) && StringUtils.isNotEmpty(dictBean.getEnName())) {
            sql += " and " + dictManager.getEnNameKey() + " like '%" + dictBean.getEnName() + "%'";
        }
        if (StringUtils.isNotEmpty(dictManager.getRemarkKey()) && StringUtils.isNotEmpty(dictBean.getRemark())) {
            sql += " and " + dictManager.getRemarkKey() + " like '%" + dictBean.getRemark() + "%'";
        }
        if (StringUtils.isNotEmpty(dictManager.getEnableKey()) && StringUtils.isNotEmpty(dictBean.getEnable())) {
            sql += " and " + dictManager.getEnableKey() + " like '%" + dictBean.getEnable() + "%'";
        }
        if (StringUtils.isNotEmpty(dictManager.getIndexKey()) && StringUtils.isNotEmpty(dictBean.getIndx())) {
            sql += " and " + dictManager.getIndexKey() + " like '%" + dictBean.getIndx() + "%'";
        }
        return sql;
    }

    /**
     * 查询字典信息
     *
     * @param dictManager
     * @param index
     * @param sort
     * @return
     */
    public BaseDictBean qryOne(TDictManager dictManager, String index, String sort) {
        StringBuffer sqlBuffer = new StringBuffer("SELECT ");
        sqlBuffer.append(generateSelect(dictManager));
        sqlBuffer.append("  FROM " + dictManager.getRealTableName() + " where  " + dictManager.getIndexKey() + "='" + index + "'" + sort + " and ROWNUM <= 1");
        return baseDictDao.qryOne(sqlBuffer.toString());
    }

    /**
     * 删除字典
     *
     * @param dictManager
     * @param indxs
     * @return
     */
    public String deleteDict(TDictManager dictManager, String indxs) {
        if (StringUtils.isEmpty(dictManager.getEnableKey())) {
            return "该字典没有状态字段，暂不提供物理删除功能";
        }
        if (StringUtils.isNotEmpty(indxs)) {
            String ids[] = indxs.split(",");
            for (String id : ids) {
                if (StringUtils.isNotEmpty(id)) {
                    baseDictDao.deleteDict(dictManager.getRealTableName(), dictManager.getEnableKey(), dictManager.getIndexKey(), id);
                }
            }
        }
        return "success";
    }

    /**
     * 保存字典内容
     *
     * @param dictManager
     * @param dictBean
     * @return
     */
    public String saveDict(TDictManager dictManager, BaseDictBean dictBean) {
        if (StringUtils.isEmpty(dictBean.getIndx())) {
            baseDictDao.insertBySql(generateInsertSql(dictManager, dictBean));
        } else {
            baseDictDao.updateBySql(generateUpdateSql(dictManager, dictBean));
        }
        return "success";
    }

    /**
     * 生成插入脚本
     *
     * @param dictManager
     * @param dictBean
     * @return
     */
    private String generateInsertSql(TDictManager dictManager, BaseDictBean dictBean) {
        String sql = "insert into " + dictManager.getRealTableName() + "(";
        String insertColumn = "";
        String insertValue = "";
        //处理主键与code相同的情况
        if (dictManager.getIndexKey().equals(dictManager.getCodeKey())) {
            insertColumn += dictManager.getIndexKey() + ",";
            insertValue += "'" + dictBean.getCode() + "',";
        } else {
            if (StringUtils.isEmpty(dictBean.getIndx())) {
                insertColumn += dictManager.getIndexKey() + ",";
                insertValue += dictManager.getDefaultIndxKey() + ",";
            }
            if (StringUtils.isNotEmpty(dictManager.getCodeKey()) && StringUtils.isNotEmpty(dictBean.getCode())) {
                insertColumn += dictManager.getCodeKey() + ",";
                insertValue += "'" + dictBean.getCode() + "',";
            }
        }
        if (StringUtils.isNotEmpty(dictManager.getCnNameKey()) && StringUtils.isNotEmpty(dictBean.getCnName())) {
            insertColumn += dictManager.getCnNameKey() + ",";
            insertValue += "'" + dictBean.getCnName() + "',";
        }
        if (StringUtils.isNotEmpty(dictManager.getEnNameKey()) && StringUtils.isNotEmpty(dictBean.getEnName())) {
            insertColumn += dictManager.getEnNameKey() + ",";
            insertValue += "'" + dictBean.getEnName() + "',";
        }
        if (StringUtils.isNotEmpty(dictManager.getRemarkKey()) && StringUtils.isNotEmpty(dictBean.getRemark())) {
            insertColumn += dictManager.getRemarkKey() + ",";
            insertValue += "'" + dictBean.getRemark() + "',";
        }
        if (StringUtils.isNotEmpty(dictManager.getEnableKey())) {
            insertColumn += dictManager.getEnableKey() + ",";
            insertValue += "'1',";
        }
        if (insertColumn.endsWith(",")) {
            insertColumn = insertColumn.substring(0, insertColumn.length() - 1);
        }
        if (insertValue.endsWith(",")) {
            insertValue = insertValue.substring(0, insertValue.length() - 1);
        }
        sql += insertColumn + ")values(" + insertValue + ")";
        return sql;
    }

    /**
     * 生成更新脚本
     *
     * @param dictManager
     * @param dictBean
     * @return
     */
    private String generateUpdateSql(TDictManager dictManager, BaseDictBean dictBean) {
        String sql = "update " + dictManager.getRealTableName() + " set ";
        String updateSql = "";
        if (StringUtils.isNotEmpty(dictManager.getCodeKey()) && StringUtils.isNotEmpty(dictBean.getCode())) {
            updateSql += dictManager.getCodeKey() + " = '" + dictBean.getCode() + "',";
        }
        if (StringUtils.isNotEmpty(dictManager.getCnNameKey()) && StringUtils.isNotEmpty(dictBean.getCnName())) {
            updateSql += dictManager.getCnNameKey() + " = '" + dictBean.getCnName() + "',";
        }
        if (StringUtils.isNotEmpty(dictManager.getEnNameKey()) && StringUtils.isNotEmpty(dictBean.getEnName())) {
            updateSql += dictManager.getEnNameKey() + " = '" + dictBean.getEnName() + "',";
        }
        if (StringUtils.isNotEmpty(dictManager.getRemarkKey()) && StringUtils.isNotEmpty(dictBean.getRemark())) {
            updateSql += dictManager.getRemarkKey() + " = '" + dictBean.getRemark() + "',";
        }
        if (StringUtils.isNotEmpty(updateSql) && updateSql.endsWith(",")) {
            updateSql = updateSql.substring(0, updateSql.length() - 1);
        }
        sql += updateSql;
        sql += " where " + dictManager.getIndexKey() + "='" + dictBean.getIndx() + "'";
        return sql;
    }
}
